﻿using System;

using System.Data.SqlClient;

namespace IFD2
{
    public partial class FltRoute : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("IA2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Request.QueryString["flt_no"] == null)
                {
                    lblError.Text = "Invalid page call!";
                }
                else
                {
                    lblAirline.Text = Session["AIRLINE"].ToString();
                    lblFltNo.Text = Request.QueryString["flt_no"].ToString();
                    lblFltDate.Text = Convert.ToDateTime(Request.QueryString["flt_date"].ToString()).ToShortDateString();
                }
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnSetArrTime_Click(object sender, EventArgs e)
        {
            SetFlightTime("A");
        }

        protected void btnSetDepTime_Click(object sender, EventArgs e)
        {
            SetFlightTime("D");
        }

        protected void SetFlightTime(string arr_dep)
        {
            if (Session["ACL"].ToString().IndexOf("IA3") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;

            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the record you wanted.";
                return;
            }

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_flt_set_schedule";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@flt_date", lblFltDate.Text);
                para = cmd.Parameters.AddWithValue("@airport", Request.Form["KEY"]);
                para = cmd.Parameters.AddWithValue("@arr_dep", arr_dep);
                para = cmd.Parameters.AddWithValue("@date_diff", lstDateDiff.SelectedValue);
                para = cmd.Parameters.AddWithValue("@time", txtTime.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Updating failed!";
                else if (ret_code == 2)
                    lblError.Text = "The time you want to set is meanlingless at this airport!";
                else if (ret_code == 3)
                    lblError.Text = "Airport city not found!";
                else if (ret_code == 4)
                    lblError.Text = "City information not found!";
                else if (ret_code == 5)
                    lblError.Text = "Convert to local time failed!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                return;
            }
        }
    }
}